var table = null;
var pid, tid;
layui
    .config({
        base: '../layui/exts/', //静态资源所在路径
    })
    .use(function () {
        var form = layui.form;
        $ = layui.$;
        table = layui.table;

        pid = $("#projectList").val();
        var util = layui.util;

        form.render();
        form.on("select(projectList)", function (data) {
            pid = data.value;
            openTaskData(table, pid);
        });

        form.on('submit(searchData)', function (data) {
            openArchivesReceive(data.field);
            return false;
        });

        openTaskData(table, pid);

        //监听工具条
        table.on("row(taskTable)", function (obj) {
            var data = obj.data;
            obj.tr.addClass("layui-table-click").siblings().removeClass("layui-table-click");
            tid = data.id;
            openArchivesReceive();
        });

        function openArchivesReceive(data) {
            let where = {pid: pid, tid: tid};
            if (data !== undefined) {
                where = Object.assign({}, where, data);
            }
            table = layui.table;
            table.render({
                elem: '#dataTable'
                , url: '/workflow/getArchivesReceiveData'
                , contentType: "application/json"
                , method: 'post'
                , cellMinWidth: 50
                , page: true
                , limit: 50
                , height: 'full-115'
                , loading: true
                , where: where
                , size: 'sm'
                , cols: [
                    [ //标题栏
                        {field: 'status', title: '状态', fixed: 'left', width: 60, templet: "#statusTpl"},
                        {field: 'batchCode', title: '批次号', fixed: 'left', width: 150},
                        {field: 'quantity', title: '接收数量', width: 60},
                        {field: 'unit', title: '单位', width: 50},
                        {field: 'outDate', title: '出库日期', width: 85},
                        {field: 'outHandover', title: '出库移交人', width: 85},
                        {field: 'outReceive', title: '出库接收人', width: 85},
                        {field: 'outVerify', title: '接收审核人', width: 85},
                        {field: 'outVerifyTime', title: '接收审核日期', width: 100},
                        {field: 'inDate', title: '归还日期', width: 85},
                        {field: 'inHandover', title: '归还移交人', width: 85},
                        {field: 'inReceive', title: '归还接收人', width: 85},
                        {field: 'inVerify', title: '归还审核人', width: 85},
                        {field: 'inVerifyTime', title: '归还审核日期', width: 100},
                        {field: 'creator', title: '创建人', width: 85},
                        {field: 'createTime', title: '创建时间', width: 135},
                        {field: 'remark', title: '备注', width: 120},
                        {fixed: 'right', title: '操作', width: 110, toolbar: '#operationBar'}
                    ]
                ],
                done: function (res, curr, count) {
                    //表格加载完成后，默认选中第一行
                    $('.layui-table-view[lay-table-id="dataTable"]').children('.layui-table-box').children('.layui-table-body').find('table tbody tr[data-index="0"]').click();
                }
            });
        }

        //监听工具条
        table.on('tool(dataTable)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case "edit":
                    top.layer.open({
                        type: 2,
                        title: '编辑项目信息',
                        content: './workflow/setArchivesReceive?id=' + data.id + '&pid=' + pid + "&tid=" + tid,
                        area: ['750px', '680px'],
                        shadeClose: false,
                        resize: false,
                        btn: [
                            '<i class="layui-icon">&#xe605;</i> 确定',
                            '<i class="layui-icon">&#x1006;</i> 取消',
                        ],
                        yes: function (index, layero) {
                            var iframeWin = layero.find('iframe')[0];
                            if (iframeWin.contentWindow.Submit()) {
                                table.reload('dataTable');
                            }
                        },
                        btn2: function (index, layero) {
                            layer.close(index); //如果设定了yes回调，需进行手工关闭
                        },
                    });
                    return false;
                case 'del':
                    top.layer.confirm('删除后将连已做数字化的档案内容一同删除，确定要删除当前选择的接收批次吗？ ', function (index) {
                        $.ajax({
                            url: '../workflow/delArchivesReceive', //url
                            contentType: 'application/json',
                            dataType: 'json', //数据格式
                            type: 'POST', //请求方式
                            async: false, //是否异步请求
                            data: JSON.stringify({id: data.id}),
                            success: function (result) {
                                if (result.code === 0) {
                                    obj.del();
                                    layer.close(index);
                                    top.parent.layer.msg(result.message, {icon: 1});
                                } else {
                                    top.parent.layer.alert(result.message, {icon: 2});
                                }
                            },
                        });
                        return false;
                    });
                case 'return':
                    //归还
                    break;
                case "detail":
                    //明细
                    break
            }
        });

        util.on({
            newReceive: function () {
                if (pid === '' || pid === undefined) {
                    top.parent.layer.alert("请选择需要创建任务的项目", {icon: 2});
                    return false;
                }
                top.layer.open({
                    type: 2,
                    title: '新增接收',
                    content: './workflow/setArchivesReceive?id=&pid=' + pid + "&tid=" + tid,
                    area: ['550px', '400px'],
                    shadeClose: false,
                    resize: false,
                    btn: [
                        '<i class="layui-icon">&#xe605;</i> 确定',
                        '<i class="layui-icon">&#x1006;</i> 取消',
                    ],
                    yes: function (index, layero) {
                        let iframeWin = layero.find('iframe')[0];
                        if (iframeWin.contentWindow.Submit()) {
                            table.reload('dataTable');
                        }
                    },
                    btn2: function (index) {
                        layer.close(index);
                    },
                });
                return false;
            }
        });
    });

